//
//  MainViewController.swift
//  1微博界面
//
//  Created by ll on 15/10/14.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    @objc private func clickComposedButton() {
            print("点我啊")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        addChildViewControllers()
        
        setupComposeButton()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    private lazy var composedButton :UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
//    {
//        let button = UIButton()
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//
//       // button.sizeToFit()
//        
//        return button
//    }()


}


extension MainViewController{
    
    private func setupComposeButton(){
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count)
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    
    }
    
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_discover")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")

        
    }

    private func addChildViewController(vc:UITableViewController , title : String , imageName :String) {
        vc.title = title;
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }



}
